/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package asa;

/**
 *
 * @author scriptoff
 */
public class ASAInstFor extends ASAInst implements NeedBlocInterface {

    private final ASAExpr expr1;
    private final ASAExpr expr2;
    private ASAInst bloc;
    private final String nomVar;
    private ASABloc bl;

    public ASAInstFor(String nomVar,ASAExpr expr1,ASAExpr expr2, ASAInst bloc) {
        this.nomVar = nomVar;
        this.expr2 = expr2;
        this.expr1 = expr1;
        this.bloc = bloc;
    }

    @Override
    public void evalue() {
        
        int i = expr1.evalue();
        int j = expr2.evalue();
        if(i > j) {
            for(int compt=i;compt>j;compt--){
                Variables v = Variables.getInstance();
                if(bloc instanceof ASABloc)
                    v.setValue(bloc, nomVar, compt);
                else 
                    v.setValue(bl, nomVar, compt);
                bloc.evalue();
            }
        } else {
            for(int compt=i;compt<j;compt++){
                Variables v = Variables.getInstance();
                if(bloc instanceof ASABloc)
                    v.setValue(bloc, nomVar, compt);
                else 
                    v.setValue(bl, nomVar, compt);
                bloc.evalue();
            }
        }
        bloc.liberer();
    }

    @Override
    public ASAExpr getExpr1() {
        return expr1;
    }

    @Override
    public ASAExpr getExpr2() {
        return expr2;
    }

    @Override
    public String getLabelExplorateur() {
        return "For";
    }

    @Override
    public void setBl(ASABloc bl) {
        this.bl = bl;
        expr1.setBl(bl);
        expr2.setBl(bl);
        
        if (bloc.getExpr1() != null) {
            try {
                NeedBlocInterface nBI = (NeedBlocInterface) bloc.getExpr1();
                nBI.setBl(bl);
            } catch (ClassCastException ex) {
            }
        }
        if (bloc.getExpr2() != null) {
            try {
                NeedBlocInterface nBI = (NeedBlocInterface) bloc.getExpr2();
                nBI.setBl(bl);
            } catch (ClassCastException ex) {
            }
        }
    }
    
}
